﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using System.Data;
using IFRS.DataAccess.DAL;
using IFRS.Models;

public partial class IFRS_SEC01_2E : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.ViewMenu();
            SetErrorMessage();
            this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }
    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/SEC01_1S.aspx");
    }

    #endregion
    #region "----- Methods -------"
    private void ViewMenu()
    {
        this.MenuTitleBar = Resources.ifrsMenu.SEC01_1S;
    }
    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        reqROLE_GROUP_NAME.SetRequiredField(lblROLE_GROUP_NAME, txtROLE_GROUP_NAME, btnSAVE);

    }

    private void BindData(int ID)
    {
        var vROLEGROUP = new ROLEGROUP();
        DataTable dtRoleGroup = vROLEGROUP.SEARCH_BYID(ID);
        List<RoleGroupU> RoleGroup = new List<RoleGroupU>();
        ConvertHelper.ConvertDataTableToObject(dtRoleGroup, RoleGroup);
        if (dtRoleGroup.Rows.Count > 0)
        {
            hidRoleGroupID.Value = RoleGroup[0].RoleGroupID.ToString();// dtRoleGroup.Rows[0]["RoleGroupID"].ToString();
            txtROLE_GROUP_NAME.Text = RoleGroup[0].RoleGroupName.ToString();//dtRoleGroup.Rows[0]["RoleGroupName"].ToString();

            if (RoleGroup[0].AllBranch.ToString().ToLower() == "true")
            {
                cbJOB.Checked = true;
            }
            if (RoleGroup[0].IsActive.ToString().ToLower() == "true")
            {
                rbSTATUS_1.Checked = true;
            }
            else
            {
                rbSTATUS_2.Checked = true;
            }
                
        }
    }
    private void SaveData()
    {

        try
        {
            bool AllBranch;
            bool IsActive;
            if (cbJOB.Checked)
            {
                AllBranch = true;
            }
            else
            {
                AllBranch = false;
            }
            if (rbSTATUS_1.Checked)
            {
                IsActive = true;
            }
            else
            {
                IsActive = false;
            }
            var vROLEGROUP = new ROLEGROUP();
            int iID = vROLEGROUP.UPDATE(hidRoleGroupID.Value.AsInt32(),txtROLE_GROUP_NAME.Text, AllBranch, IsActive, DateTime.Now, SessionManager.IFRS_EMPLOYEE_ID,
                                        SessionManager.IFRS_COST_CENTER, SessionManager.IFRS_BRANCH, DateTime.Now, SessionManager.IFRS_EMPLOYEE_ID,
                                        SessionManager.IFRS_COST_CENTER, SessionManager.IFRS_BRANCH);
            if (iID > 0)
            {
                // Utility.AlertMessage(Page, Resources.ifrsResource.SaveData);
                this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/SEC01_2E.aspx?&id={0}", hidRoleGroupID.Value.AsInt32())));
                //Response.Redirect(string.Format("~/IFRS/SEC01_2E.aspx?id={0}", iID)); 
            }
            else
            {
                Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    #endregion
 
}